package com.maisha.Controller.cms;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.maisha.Service.IService.ICMSPowerService;
import com.maisha.base.Controller.BaseController;
import com.maisha.bean.AuditPower;
import com.maisha.form.PowerForm;

@Controller(value = "cmsPowerManagerController")
@RequestMapping(value="cms")
public class CMSPowerManagerController extends BaseController {

	@Resource
	private ICMSPowerService cmsPowerServiceImp;
	
	/** 获得权限的列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="getPowerList")
	public ModelAndView getPowerList(HttpServletRequest request, HttpServletResponse response){
		return getPowerList();
	}
	
	/** 添加权限管理的页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="addPower")
	public ModelAndView addPower(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("jsp/cms/sysManager/addPower");
		return mv;
	}
	
	/** 添加权限信息
	 * @param request
	 * @param response
	 * @param powerForm
	 * @return
	 */
	@RequestMapping(value="toAddPower")
	public ModelAndView toAddPower(HttpServletRequest request, HttpServletResponse response,PowerForm powerForm){
		AuditPower audit = new AuditPower();
		audit.setName(powerForm.getName());
		audit.setCode(powerForm.getCode());
		audit.setLevel(powerForm.getLevel());
		audit.setParentId(powerForm.getParentId());
		cmsPowerServiceImp.addAuditPower(audit);
		return getPowerList();
	}
	
	/** 更具 级别获得全新信息 封装成josn 写到前台
	 * @param request
	 * @param response
	 * @param powerForm
	 */
	@RequestMapping(value="getPowerByLevel")
	public void getPowerByLevel(HttpServletRequest request, HttpServletResponse response,PowerForm powerForm){
		try {
			String result = cmsPowerServiceImp.getAuditPowerByLevel(powerForm);
			PrintWriter out = response.getWriter();
			out.write(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/** 获得权限的列表
	 * @return
	 */
	private ModelAndView getPowerList(){
		ModelAndView mv = new ModelAndView();
		List<AuditPower> result = cmsPowerServiceImp.getAuditPowerList();
		mv.addObject("result",result);
		mv.setViewName("jsp/cms/sysManager/powerList");
		return mv;
	}
	
	
}
